﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TestSystem.BLL;
using TestSystem.BLL.TestSystem;


public partial class Admin_ManageQuizes : System.Web.UI.Page
{
    private int subjectID = 0;
    public int SubjectID
    {
        get
        {
            if (subjectID == 0 && Request.QueryString["subjectID"] != null)
                subjectID = Convert.ToInt32(Request.QueryString["subjectID"]);
            return subjectID;
        }
    }

    private List<Quiz> quizes = null;
    public List<Quiz> Quizes
    {
        get
        {
            if (quizes == null && SubjectID != 0)
                quizes = (List<Quiz>)quizesObjSource.Select();
            return quizes;
        }
        set
        {
            quizes = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void quizesObjSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        e.InputParameters["addedBy"] = Utils.CurrentUserName;
        if(SubjectID == 0)
            e.Cancel = true;
        e.InputParameters["subjectID"] = SubjectID;
    }

    protected void lvwQuizes_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        e.Cancel = true;
        int quizId = Convert.ToInt32(e.Keys["QuizID"]);
        var currQuiz = Quizes.Find(quiz => quiz.QuizID == quizId); 
        if(currQuiz != null)
        {
            currQuiz.SubjectID = int.Parse((lvwQuizes.Items[e.ItemIndex].FindControl("ddlSubjects") as DropDownList).SelectedValue);
            currQuiz.Active = Convert.ToBoolean(e.NewValues["Active"]);
            currQuiz.AddedDate = Convert.ToDateTime(e.NewValues["AddedDate"]);
            currQuiz.Deleted = Convert.ToBoolean(e.NewValues["Deleted"]);
            currQuiz.Description = e.NewValues["Description"] == null ? null : e.NewValues["Description"].ToString();
            currQuiz.QuestionsCount = Convert.ToInt32(e.NewValues["QuestionsCount"]);
            currQuiz.SaveUserAnswers = Convert.ToBoolean(e.NewValues["SaveUserAnswers"]);
            currQuiz.ScoreToPass = Convert.ToInt32(e.NewValues["ScoreToPass"]);
            currQuiz.TimePerQuiz = Convert.ToInt32(e.NewValues["TimePerQuiz"]);
            currQuiz.Title = e.NewValues["Title"].ToString();
            currQuiz.UseTimer = Convert.ToBoolean(e.NewValues["UseTimer"]);
            currQuiz.MaxScores = Convert.ToInt32(e.NewValues["MaxScores"]);
            currQuiz.AllowReAnswer = Convert.ToBoolean(e.NewValues["AllowReAnswer"]);
            currQuiz.Update();
            lvwQuizes.EditIndex = -1;
            lvwQuizes.DataBind();
            Quizes = null;
        }
    }

    protected void lvwQuizes_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if(e.CommandName == "Restore")
        {
            int quizId = Convert.ToInt32(e.CommandArgument);
            var currQuiz = Quizes.Find(quiz => quiz.QuizID == quizId);
            if(currQuiz != null)
            {
                currQuiz.Deleted = false;
                currQuiz.Update();
                lvwQuizes.DataBind();
                Quizes = null;
            }
        }
        if(e.CommandName == "AddTest")
        {
            int quizId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("ManageTests.aspx?quizID=" + quizId + "#insert");
        }
        if(e.CommandName == "Tests")
        {
            int quizId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("ManageTests.aspx?quizId=" + quizId);
        }
    }
    
    protected void lvwQuizes_ItemInserted(object sender, ListViewInsertedEventArgs e)
    {
        Quizes = null;
    }

    protected void lvwQuizes_ItemDeleted(object sender, ListViewDeletedEventArgs e)
    {
        Quizes = null;
    }
    
    protected void lvwQuizes_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        var ddlSubjects = e.Item.FindControl("ddlSubjects") as DropDownList;
        if (ddlSubjects != null)
        {
            ddlSubjects.DataSource = (from subject in Subject.GetAll(false)
                                      select new {
                                                     SubjectId = subject.SubjectID,
                                                     Name = subject.SubjectName
                                                 }).ToList();
            ddlSubjects.DataBind();
            ddlSubjects.SelectedValue = SubjectID.ToString();
        }
    }
}
